package com.doit.day03;

import lombok.*;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

//需求：自己写一个迭代器，去读取Hero.txt这个文件，然后每调用一次，就返回一个对象,对象就是一行数据的封装
public class HeroIterator implements Iterator<Hero> {
    private String line;
    private BufferedReader bufferedReader;
    //1.读文件
    //2.读取一行内容
    //3.将内容进行切割，封装成对象返回
    public HeroIterator() throws Exception {
        //1.打开文件
        bufferedReader = new BufferedReader(new FileReader("D:\\develop\\ideaWorkSpace\\myself\\Teach\\scala39\\src\\data\\Hero.txt"));
        line = bufferedReader.readLine();
    }
    /*
    判断是否有下一行数据
     */
    @Override
    public boolean hasNext() {
        //读取了一行数据
        return line != null;
    }

    @Override
    public Hero next() {
        String res = line;
        try {
            line = bufferedReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //1_张飞,10::120
        //1    张飞,10::120
        String[] arr = res.split("_");
        //张飞,10::120
        //张飞      10::120
        String[] arr1 = arr[1].split(",");
        //10::120
        //10    120
        String[] arr2 = arr1[1].split("::");

        return new Hero(Integer.parseInt(arr[0]),arr1[0],Integer.parseInt(arr2[0]),Double.parseDouble(arr2[1]));
    }
}


@AllArgsConstructor
@NoArgsConstructor
@ToString
@Getter
@Setter
class Hero{
    private Integer id;
    private String name;
    private Integer faceValue;
    private Double power;
}
